from pyalgotrade import plotter
from pyalgotrade.stratanalyzer import returns

from trade.trade_strategy import Feed
from trade.trade_strategy import SMACrossOver

if __name__ == '__main__':
    feed = Feed()
    feed.addBarsFromCSV("sz.000875", "./data/company-day/sz.000875-吉电股份.csv")

    # Evaluate the strategy with the feed's bars.
    myStrategy = SMACrossOver(feed, "sz.000875", 10)
    # myStrategy.run()

    # Attach a returns analyzers to the strategy.
    returnsAnalyzer = returns.Returns()
    myStrategy.attachAnalyzer(returnsAnalyzer)

    # Attach the plotter to the strategy.
    plt = plotter.StrategyPlotter(myStrategy)
    # Include the SMA in the instrument's subplot to get it displayed along with the closing prices.
    plt.getInstrumentSubplot("sz.000875").addDataSeries("SMA", myStrategy.getSMA())
    # Plot the simple returns on each bar.
    plt.getOrCreateSubplot("returns").addDataSeries("Simple returns", returnsAnalyzer.getReturns())

    # Run the strategy.
    myStrategy.run()
    myStrategy.info("Final portfolio value: $%.2f" % myStrategy.getResult())

    # Plot the strategy.
    plt.plot()
    print("over---:")
    print("Final portfolio value: $%.2f" % myStrategy.getBroker().getEquity())

# if __name__ == '__main__':
# import tensorflow.compat.v1 as tf
# tf.disable_v2_behavior()
# import numpy as np
#
# ## 创建测试数据
# x_data = np.random.rand(100).astype(np.float32)
# y_data = x_data * 0.1 + 0.3
# print("x_data", x_data)
# print("y_data", y_data)
# print("-------------------------")
# ###----------创建结构开始----------###
# # 搭建模型
# Weights = tf.Variable(tf.compat.v1.random_uniform([1], -1, 1.0))
# biases = tf.Variable(tf.zeros([1]))
#
# y = x_data * Weights + biases
#
# # 计算误差
# loss = tf.reduce_mean(tf.square(y - y_data))
#
# # 反向传递，优化权重和偏置
# optimizer = tf.compat.v1.train.GradientDescentOptimizer(0.5)
# train = optimizer.minimize(loss)
#
# ###----------创建结构结束----------###
#
# # 初始化结构
# init = tf.compat.v1.global_variables_initializer()
# # 获取Session
# sess = tf.compat.v1.Session()
# # 用Session进行初始化
# sess.run(init)
#
# # 开始训练
# for step in range(201):
#     sess.run(train)
#     # 每20步输出权重和偏置
#     if step % 20 == 0:
#         print(step, sess.run(Weights), sess.run(biases))
#
# pass
